package main.java.main.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;

import com.csvreader.CsvReader;

/*
 *包名:com.kdzwy.cases
 *作者:Adien_cui
 *时间:2017-9-25  下午4:36:29
 *描述:读取csv文件
 **/
public class ReadCsvFile {
    public static  ArrayList<String[]> readCsvFile(String filePath){
        ArrayList<String[]> csvList = new ArrayList<String[]>();
        try {

            CsvReader reader = new CsvReader(filePath,',',Charset.forName("UTF-8"));
//          reader.readHeaders(); //跳过表头,不跳可以注释掉

            while(reader.readRecord()){
                csvList.add(reader.getValues()); //按行读取，并把每一行的数据添加到list集合
            }
            reader.close();
            System.out.println("读取的Csv行数："+csvList.size());

            return csvList;
//
//            for(int row=0;row<csvList.size();row++){
//                System.out.println("-----------------");
//                //打印每一行的数据
//                System.out.print(csvList.get(row)[0]+",");
//                System.out.print(csvList.get(row)[1]+",");
//                System.out.print(csvList.get(row)[2]+",");
//                System.out.println(csvList.get(row)[3]+",");
//                //如果第一列（即姓名列）包含lisa，则打印出lisa的年龄
//                if(csvList.get(row)[0].equals("lisa")){
//                    System.out.println("lisa的年龄为："+csvList.get(row)[2]);
//                }
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return csvList;
    }

    public static void main(String[] args) {
        String filePath = "F:\\data.csv";
        readCsvFile(filePath);
    }
}